<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * Created by PhpStorm.
 * User: zhengzifan
 * Date: 2017/3/16
 * Time: 13:41
 */

function getAlert($msg, $url=null){
    $html = '';
    $html .= "<script>";
    if($msg){
        $html .= "alert('{$msg}');";
    }
    if($url){
        $html .= "window.location.href='{$url}';";
    }
    $html .= "</script>";
    echo $html;
}

/**
 * @param $info
 * 写入管理员操作日志
 */
function admin_log($info){
    db('AdminLog')->insert([
        'admin_id' => session('admin')['id'],
        'log_info' => $info,
        'log_ip' => $_SERVER['REMOTE_ADDR'],
        'log_time' => time(),
    ]);
}
function actionRead($filename, $encode = 'utf-8'){
    if(strpos($filename,'.xlsx')){
        $objReader = PHPExcel_IOFactory::createReader('Excel2007'); 
    }else{
        $objReader = PHPExcel_IOFactory::createReader('Excel5'); 
    }
    $objReader->setReadDataOnly(true);
    $objPHPExcel = $objReader->load($filename);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $highestRow = $objWorksheet->getHighestRow();
    $highestColumn = $objWorksheet->getHighestColumn();
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $excelData = array();
    for($row = 1; $row <= $highestRow; $row++){
        for ($col = 0; $col < $highestColumnIndex; $col++){
            $excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
        }
    }

    return $excelData;
}

function extractImageFromWorksheet($filename){
    $basePath = ROOT_PATH.'public'.DS.'uploads'.DS.date('Ymd').DS;
    $uploadPath = DS.'public'.DS.'uploads'.DS.date('Ymd').DS;
    if(strpos($filename,'.xlsx')){
        $objReader = PHPExcel_IOFactory::createReader('Excel2007'); 
    }else{
        $objReader = PHPExcel_IOFactory::createReader('Excel5'); 
    }
    $objReader->setReadDataOnly(false);
    $objPHPExcel = $objReader->load($filename);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $result = array();
    $imageFileName = "";
    foreach ($objWorksheet->getDrawingCollection() as $drawing) {
        list($columnInd, $rowInd) = PHPExcel_Cell::coordinateFromString($drawing->getCoordinates());
        $xy = $drawing->getCoordinates();
        $path = $basePath;
        $file_url = $uploadPath;
        $fileName = md5(microtime(true));
        // for xlsx
        if ($drawing instanceof PHPExcel_Worksheet_Drawing) {
            $filename = $drawing->getPath();
            $last_filename = substr($filename,-7);
            $file_ext = substr($last_filename,strpos($last_filename,'.'));
            $imageFileName = $drawing->getIndexedFilename();
            $path = $path . $fileName.$file_ext;
            copy($filename, $path);
            $file_url .= $fileName.$file_ext;
        // for xls
        } else if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
            $image = $drawing->getImageResource();
            $renderingFunction = $drawing->getRenderingFunction();
            switch ($renderingFunction) {
                case PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG:
                    $imageFileName = $drawing->getIndexedFilename();
                    $path = $path . $fileName.'.jpg';
                    imagejpeg($image, $path);
                    $file_url .= $fileName.'.jpg';
                    break;
                case PHPExcel_Worksheet_MemoryDrawing::RENDERING_GIF:
                    $imageFileName = $drawing->getIndexedFilename();
                    $path = $path . $fileName.'.gif';
                    imagegif($image, $path);
                    $file_url .= $fileName.'.gif';
                    break;
                case PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG:
                    $imageFileName = $drawing->getIndexedFilename();
                    $path = $path . $fileName.'.png';
                    imagegif($image, $path);
                    $file_url .= $fileName.'.png';
                    break;
                case PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT:
                    $imageFileName = $drawing->getIndexedFilename();
                    $path = $path . $fileName.'.jpg';
                    imagegif($image, $path);
                    $file_url .= $fileName.'.jpg';
                    break;
            }
        }
        $result[$columnInd.','.$rowInd] = $file_url;
    }
    return $result;
}
function searchArray($list,$key,$value,$mode_id = 0){
    $rvalue = [];
    if($mode_id == 0){
        foreach($list as $item){
            if($item[$key] == $value){
                $rvalue = $item;
                return $rvalue;
            }
        }
    }else{
        foreach($list as $key=>$val){
            if($val == $value){
                $rvalue['id'] = $key;
                return $rvalue;
            }
        }
    }
    $rvalue['id'] = 0;
    return $rvalue;
}

function excelTime($days,$time=false)
{
    if(is_numeric($days))
    {
        $jd = GregorianToJD(1, 1, 1970);
        $gregorian = JDToGregorian($jd+intval($days)-25569);
        $gregorian = strtotime($gregorian);
        $gregorian = date("Y-m-d",$gregorian);
        return $gregorian;
    }
    return $time;
}